﻿using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;

namespace QuanLyHopDong.Web.Common
{
    public class WebCommon
    {
        private static Employee _employee;
        public static Employee CurrrentEmployee
        {
            get
            {
                _employee = (Employee)HttpContext.Current.Session["CurrentEmployee"];
                if (_employee == null)
                {
                    if (HttpContext.Current.User != null)
                        _employee = new EmployeeBiz().GetByUserName(HttpContext.Current.User.Identity.Name);
                    HttpContext.Current.Session["CurrentEmployee"] = _employee;
                }
                return _employee;
            }

            set
            {
                if (value == null)
                {
                    HttpContext.Current.User = null;
                    HttpContext.Current.Session.Remove("CurrentEmployee");
                    return;
                }
                HttpContext.Current.Session["CurrentEmployee"] = value;
            }
        }

        public static string GetGlobalString(string key)
        {
            return (string)HttpContext.GetGlobalResourceObject("GlobalStrings", key);
        }

        public static void UploadFile(int hopDongId, FileUpload fileHopDong)
        {
            if (!fileHopDong.HasFile) return;
            if (hopDongId <= 0) return;

            var fileName = Path.GetFileName(fileHopDong.PostedFile.FileName);
            var folderPath = HttpContext.Current.Server.MapPath(string.Format("~/Files/HopDong/{0}", hopDongId));
            // create folder
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            // save file
            var filePathFull = folderPath + "/" + fileName;
            if (File.Exists(filePathFull))
                File.Delete(filePathFull);
            fileHopDong.PostedFile.SaveAs(filePathFull);
        }

        public static string[] TrangThaiHopDongStrings = { "", "Chưa ký hợp đồng", "Đã ký, đang thi công", "Đang hoàn công", "Đang quyết toán", "Hoàn thiện" };
    }
}